package com.kuaiditu.assistant.ui.splash;

import com.alibaba.android.arouter.launcher.ARouter;
import com.kuaiditu.assistant.R;
import com.kuaiditu.assistant.api.ParamsAnalog;
import com.kuaiditu.assistant.api.assistant_api.AssistantApi;
import com.kuaiditu.assistant.base.BaseActivity;
import com.kuaiditu.assistant.entity.AssistantUser;
import com.kuaiditu.assistant.module.storage.AssistantUserStorage;
import com.kuaiditu.assistant.ui.login.LoginHintActivity;
import com.kuaiditu.assistant.ui.main.MainActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

public class SplashActivity extends BaseActivity {

    private AssistantUserStorage mAssistantUserStorage;

    private ParamsAnalog mParamsAnalog;

    private AssistantApi mAssistantApi;






    @Override
    public int initContentView() {
        return R.layout.activity_splash;
    }

    @Override
    public void initInjector() {
        mAssistantApi = getApplicationComponent().getApiManager().getAssistantApi();
        mParamsAnalog = getApplicationComponent().getParamsAnalog();
        mAssistantUserStorage = getApplicationComponent().getUserStorage();
    }

    @Override
    public void initUiAndListener() {

        if (mAssistantUserStorage.isLogin()) {
            refreshUser();
        }


        Observable.timer(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<Long>() {
                    @Override
                    public void onNext(@NonNull Long aLong) {

                        if (mAssistantUserStorage.isLogin()) {
                            ARouter.getInstance().build(MainActivity.PATH).navigation();
                        } else {
                            ARouter.getInstance().build(LoginHintActivity.PATH).navigation();
                        }
                        finish();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    private void refreshUser() {
        mAssistantApi.refreshUser()
                .subscribe(new DefaultObserver<AssistantUser>() {
                    @Override
                    public void onNext(@NonNull AssistantUser assistantUser) {

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected boolean setTranslucent() {
        return true;
    }


    @Override
    protected boolean setStatusBar() {
        return false;
    }

    @Override
    public void onBackPressed() {

    }
}
